package com.sctvcloud.dongqu.ui.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.base.BaseFragment;
import com.sctvcloud.dongqu.beans.LiveItem;
import com.sctvcloud.dongqu.ui.activities.LivingActivity;
import com.sctvcloud.dongqu.ui.fragment.LiveFragment;
import com.sctvcloud.dongqu.ui.utils.GlideUtil;
import com.sctvcloud.dongqu.ui.widget.BasePlayerView;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by liuyu on 2017/7/14.
 */

public class LiveHomeItemHolder extends BaseAbsHolder<LiveItem> implements View.OnClickListener {
    @BindView(R.id.video_player)
    public BasePlayerView videoPlayer;
    @BindView(R.id.item_live_play)
    public View playerBg;

    @BindView(R.id.bottom_layout)
    public LinearLayout bottom;
    @BindView(R.id.item_live_item_img)
    protected CustomEXImageView img;
    @BindView(R.id.item_live_list_mode)
    protected CustomFontTextView mode;
    @BindView(R.id.item_live_item_title)
    public CustomFontTextView title;
    @BindView(R.id.item_live_label_date)
    protected CustomFontTextView date;
    @BindView(R.id.item_live_label_channel)
    protected CustomFontTextView channel;
    private Context context;
    private LiveFragment liveFragment;
    private LiveItem liveItem;
    private LiveItemClickListener listener;
    private int position;

    public LiveHomeItemHolder(Context context, View itemView, LiveItemClickListener li, BaseFragment fragment) {
        super(context, itemView);
        this.context = context;
//        itemView.setOnClickListener(this);
        listener = li;
        if (fragment instanceof LiveFragment) {
            this.liveFragment = (LiveFragment) fragment;
        }
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(final LiveItem liveItem, final int position, boolean isSelected) {
        super.setData(liveItem, position, isSelected);
        playerBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.OnItemClick(liveItem, position);
                }
            }
        });
    }

    @Override
    public void setData(final LiveItem item) {
        this.liveItem = item;
        if (liveItem.isLiving()) {
            mode.setBackgroundResource(R.drawable.living_bg);
            mode.setText(R.string.livinging);
        } else {
            mode.setBackgroundResource(R.drawable.live_review_bg_d);
            mode.setText(R.string.retrospect);
        }
        mode.setVisibility(View.VISIBLE);
        title.setText(liveItem.getTitle());
        date.setText(DateUtils.formatDate(DateUtils.getDataDate(liveItem), ""));
        channel.setText(liveItem.getLiveSource());
//        if (!TextUtils.isEmpty(liveItem.getLiveImg())) {
//            GlideUtil.loadImgLarge169Def(context, liveItem.getLiveImg(), img);
//        }

        Handler handler = new Handler();
        if (null != videoPlayer) {
            videoPlayer.setLiving(liveItem.isLiving());
            videoPlayer.setMute(liveItem.isMute());
            videoPlayer.setMuteChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    liveItem.setMute(isChecked);
                }
            });
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (!TextUtils.isEmpty(liveItem.getLivePlayUrl())) {
                        videoPlayer.setVisibility(View.VISIBLE);
                        img.setVisibility(View.GONE);
                        if (videoPlayer != null && videoPlayer.getThumbImge() != null) {
                            GlideUtil.getGlideWithLarge169DefNotFit(context, liveItem.getLiveImg()).into(videoPlayer.getThumbImge());
                        }
                        if (!TextUtils.isEmpty(liveItem.getLivePlayUrl())) {
                            videoPlayer.setUp(liveItem.getLivePlayUrl(), false, "");
                        }
                    } else {
                        videoPlayer.setVisibility(View.GONE);
                        img.setVisibility(View.VISIBLE);
                        GlideUtil.getGlideWithLarge169DefNotFit(context, liveItem.getLiveImg()).into(img);
                    }
                }
            }, 200);
            if (null != listener && !TextUtils.isEmpty(liveItem.getLivePlayUrl())) {
                listener.OnItemInit(videoPlayer, position);
            }
        }
    }

    public String playVideo() {
        if (null != videoPlayer && !TextUtils.isEmpty(liveItem.getLivePlayUrl())) {
//            videoPlayer.startPlay();
            videoPlayer.startPlayLogic();
            videoPlayer.setEnabled(true);
            return liveItem.getLiveId();
        } else {
            return "";
        }
    }

    public void pauseVideo() {
        if (null != videoPlayer && !TextUtils.isEmpty(liveItem.getLivePlayUrl())) {
//            videoPlayer.onVideoPause();
            videoPlayer.onVideoPause();
            videoPlayer.setEnabled(false);
            videoPlayer.release();
        }
    }

    public void exchangeStatus() {
        if (null != videoPlayer) {
            if (videoPlayer.isPlaying()) {
                videoPlayer.onVideoPause();
            } else {
                videoPlayer.startPlay();
            }
        }
    }

    public boolean isPlaying() {
        if (null != videoPlayer) {
            return videoPlayer.isPlaying();
        } else {
            return false;
        }
    }

    public BasePlayerView getVideoPlayer() {
        return videoPlayer;
    }

    public LiveItem getLiveItem() {
        return liveItem;
    }

    @OnClick({R.id.bottom_layout})
    public void onClick(View v) {
//        if(null!=listener){
//            listener.OnItemClick(liveItem);
//        }
        Intent intent = new Intent(context, LivingActivity.class);
//		if (liveItem != null && liveItem.isLiving()) {
        intent.putExtra(BaseActivity.EX_URL, liveItem.getLiveUrl());
        intent.putExtra(BaseActivity.EX_DATA, liveItem.getLiveImg());
        if (liveItem.isLiving()) {
            pairs.add(new Pair<String, String>("位置", "正在直播"));
        } else {
            pairs.add(new Pair<String, String>("位置", "直播回顾"));
        }
        ArrayList<Pair<String, String>> pairArrayList = new ArrayList<>();
        pairArrayList.addAll(pairs);
        pairArrayList.add(new Pair<String, String>("行为类型", "点击"));
        if (fragment != null) {
            GridsumWebDissector.getInstance().trackEvent(fragment, "", liveItem.getTitle(), "", 200, pairArrayList);
        } else if (activity != null) {
            GridsumWebDissector.getInstance().trackEvent(activity, "", liveItem.getTitle(), "", 200, pairArrayList);
        }
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> pair : pairs) {
            map.put(pair.first, pair.second);
        }
        intent.putExtra("TAG", map);
        context.startActivity(intent);

    }

    public void setPosition(int position) {
        this.position = position;
    }

    public void setListener(LiveItemClickListener listener) {
        this.listener = listener;
    }

    public interface LiveItemClickListener {
        void OnItemClick(LiveItem item, int position);

        void OnItemInit(BasePlayerView player, int position);
    }
}
